package com.csii.adhoc.dataunits.service.impl;

import com.csii.adhoc.dataunits.UnitBase.UnitBasePo;
import com.csii.adhoc.dataunits.mapper.UnitBaseMapper;
import com.csii.adhoc.dataunits.service.UnitBaseService;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.id.UidGenerator;
import com.csii.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @description:
 * @author: panChuanJiang
 * @create: 2021-02-20 09:08
 */
@Service("unitBaseService")
@Slf4j
public class UnitBaseServiceImpl implements UnitBaseService {

    @Resource
    private UnitBaseMapper unitBaseMapper;
    @Resource
    private UidGenerator uidGenerator;

    @Override
    public String unitBaseMaintain(UnitBasePo po, String optMode) {
        switch (optMode) {
            case CommonConstant.OPT_MODE_ADD:
                po.setCreatedBy(po.getUpdatedBy())
                        .setCreatedDt(po.getUpdatedDt());
                return insertOneUnitBase(po);
            case CommonConstant.OPT_MODE_DELETE:
                deleteUnitBaseByPrimaryKey(po.getUnitId());
                return po.getUnitId();
            case CommonConstant.OPT_MODE_UPDATE:
                updateUnitBaseByPrimaryKey(po);
                return po.getUnitId();
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("optMode值不合法:" + optMode);
        }
    }

    @Transactional
    public void updateUnitBaseByPrimaryKey(UnitBasePo po) {
        unitBaseMapper.updateUnitBaseByPrimaryKey(po);
    }

    @Transactional
    public void deleteUnitBaseByPrimaryKey(String unitId) {
        unitBaseMapper.deleteUnitBaseByPrimaryKey(unitId);
    }

    @Transactional
    public String insertOneUnitBase(UnitBasePo po) {
        unitBaseMapper.insertOneUnitBase(po);
        return po.getUnitId();
    }
}
